package dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import entity.User;
import util.HibernateUtil;


public class Impldao implements  Userdao{
	
	public Impldao() {
	
	}

	public boolean add(User user) throws Exception{
		Session session=null;
		Transaction tran = null;
		boolean result = false;
		try
		{
			session=HibernateUtil.getSession();
			tran = session.beginTransaction();
			session.save(user);
			tran.commit();
			result=true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(tran != null)
				tran.rollback();
		}
		finally
		{
			if(session!=null)
			{
				session.close();
			}
		}
		return result;
	}
	//删除一条user表记录

	public boolean delete(User user) throws Exception {
		Session session=null;
		Transaction tran = null;
		try
		{
			session=HibernateUtil.getSession();
			tran = session.beginTransaction();
			session.delete(user);
			tran.commit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(tran != null)
				tran.rollback();
		}
		finally
		{
			if(session!=null)
			{
				session.close();
			}
		}
		return true;
	}
	//修改一条user表记录

	public boolean update(User user) throws Exception{
		Session session=null;
		Transaction tran = null;
		try
		{
			session=HibernateUtil.getSession();
			tran = session.beginTransaction();
			session.update(user);
			tran.commit();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(tran != null)
				tran.rollback();
		}
		finally
		{
			if(session!=null)
			{
				session.close();
			}
		}
		return true;
	}
	//返回user表所有数据

	@SuppressWarnings("unchecked")
	public List<User> findall() throws Exception {
		  {
			  Session session=null;
			  List<User> list=null;

			   try
				{
				   session=HibernateUtil.getSession();
				   Criteria c=session.createCriteria(User.class);
				   list=c.list();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					if(session!=null)
					{
						session.close();
					}
				}
			    
				return list;
		  }
		
		
	}
	public boolean find(String username) throws Exception {
		  {
			  Session session=null;
			  User result = null;
			   try
				{
				   session=HibernateUtil.getSession();
				   String hql = "from User user where user.username=:username";
					Query query = session.createQuery(hql);
					query.setParameter("username",username);
					result = (User)query.uniqueResult();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					if(session!=null)
					{
						session.close();
					}
				}
			    
				if (result!=null) return true;
				else return false;
		  }
		
		
	}
	public User findid(String username) throws Exception{
		 Session session=null;
		  User result = null;
		   try
			{
			   session = HibernateUtil.getSession();
				String hql = "from User user where user.username=:username";
				Query query = session.createQuery(hql);
				query.setParameter("username",username);
				result = (User)query.uniqueResult();
			}
			catch (Exception e)
			{
			}
			finally
			{
				if(session!=null)
				{
					session.close();
				}
			}
			return result;
			
	}
	
}